package city.spring.modules.oldman.service.impl;

import city.spring.modules.oldman.entity.OldmanUtilitybillEntity;
import city.spring.modules.oldman.repository.OldmanUtilitybillRepository;
import city.spring.modules.oldman.service.OldmanUtilitybillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Service：老人水电费
 *
 * @author 谢金婷
 * @date 2020-09-05 23:14:23
 */
@CacheConfig(cacheNames = {OldmanUtilitybillServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Exception.class)
@Service
public class OldmanUtilitybillServiceImpl extends ServiceImpl<OldmanUtilitybillRepository, OldmanUtilitybillEntity> implements OldmanUtilitybillService {

    private final OldmanUtilitybillRepository oldmanUtilitybillRepository;

    public OldmanUtilitybillServiceImpl(OldmanUtilitybillRepository oldmanUtilitybillRepository) {
        this.oldmanUtilitybillRepository = oldmanUtilitybillRepository;
    }

    @Override
    public void saveOldmanUtilitybill(OldmanUtilitybillEntity entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
    }

    @Override
    public void updateOldmanUtilitybill(OldmanUtilitybillEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        if (!updateById(entity)) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteOldmanUtilitybill(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteOldmanUtilitybill(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public List<OldmanUtilitybillEntity> getUtilitybillByOldmanId(String oldmanId) {
        return oldmanUtilitybillRepository.getUtilitybillByOldmanId(oldmanId);
    }
}